#!/usr/bin/env python
#coding:utf-8
# author : wangwei
# mail   ：alsww@qq.com
# oldboy s10 python training

import sys

from menu import menuShow

while True:
    user = str(raw_input("\033[1;32;40m请输入您的用户名:\033[0m")).strip()
    if len(user) == 0: continue   #输入为空就继续输入
    #登录之前判定是否是锁定用户
    f_lock = file('user_lock.txt','r')
    for line in f_lock.readlines():
        lock_user = line.split()[0]
        if user == lock_user:
            print "抱歉，该用户已经锁定，请联系管理员解锁！"
            f_lock.close()
            sys.exit()
    #如果帐号为管理员，则登录进入管理员模式，可以解锁锁定的用户。
    if user == 'admin':
        print "管理员您好，欢迎登录本ATM系统，您有权操作解锁已锁的账户，请谨慎操作！"
        f = open('user_list.txt','r')
        num = 0
        for line in f.readlines():
            u = str(line.split()[0])   #列出用户名
            p = str(line.split()[1])   #列出密码
            pwd = raw_input("请输入的密码").strip()
            if len(pwd) == 0: continue
            if pwd == p:
                while True:
                    unlock_user = raw_input("请输入解锁的账户名：").strip()
                    f_lock = file('user_lock.txt','r')
                    for line in f_lock.readlines():
                        lock_user = line.split()[0]
                        if unlock_user == lock_user:
                            f_lock = file('user_lock.txt','w+')
                            f_lock.write("")
                            print "解锁成功，将退出系统，请重新登录！"
                            f_lock.close()
                            sys.exit()
                        else:
                            print "抱歉解锁用户不存在！"
                            break
            else:
                print "密码错误，请重新输入！"
    f = open('user_list.txt','r')
    num = 0
    for line in f.readlines():
        u = str(line.split()[0])   #列出用户名
        p = str(line.split()[1])   #列出密码
        if user == u:
            while num < 3:
                password = str(raw_input("\033[1;32;40m请输入您的密码:\033[0m"))
                if len(password) == 0: continue
                if password != p:
                    print "\033[1;31;40m抱歉，您输入的密码错误！\033[0m"
                    num += 1
                    continue
                else:
                    print "\033[1;32;40m您输入密码正确！.\033[0m"
                    global money
                    money_total = str(line.split()[2])
                    while True:
                        print "\033[1;33;40m您账户中总计有: \033[0m ,\033[1;31;40m%s\033[0m" % money_total, "元！"
                        money_total = menuShow(user,money_total)
            else:
                if user == 'admin':
                    print '管理员不能被锁定'
                    break
                print "您输入的密码次数超过3次，该用户将被锁定，若需解锁，请联系管理员！"

                f_lock = file('user_lock.txt','a')
                f_lock.write(user)
                f_lock.close()
                sys.exit()
    else:
        print "\033[1;31;40m用户不存在，请重新输入！:\033[0m"
        continue